<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" :close-on-click-modal='false' class="bonus">
    <el-row>
      <el-form class="col-12" :disabled="!isEdit" :model="formData" ref="formRef" :label-width="labelWidth"  :rules="rules" v-loading="formLoading" :close-on-click-modal="false">
        <el-form-item label="机构名称" prop="deptName">
          <el-input v-model="formData.deptName" autocomplete="off" placeholder="请输入机构名称" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="机构编号" prop="deptCode">
          <el-input v-model="formData.deptCode" autocomplete="off" placeholder="请输入机构编号" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="机构系数" prop="deptRatio" >
          <el-input v-model="formData.deptRatio" autocomplete="off" placeholder="请输入机构系数" clearable disabled></el-input>
        </el-form-item>
        <el-form-item label="考核项" prop="assessItem" >
          <el-input v-model="formData.assessItem" autocomplete="off" placeholder="请输入考核项" clearable></el-input>
        </el-form-item>
        <el-form-item label="考核时间" prop="assessDate" >
          <el-date-picker type="date" placeholder="选择考核时间" v-model="formData.assessDate" format="yyyy-MM-dd"  value-format="yyyy-MM-dd" style="width: 100%;" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="考核得分" prop="assessScore" >
          <el-input v-model="formData.assessScore" autocomplete="off" placeholder="请输入考核得分" clearable></el-input>
        </el-form-item>
        <el-form-item label="考核评级" prop="assessGrade" >
          <el-input v-model="formData.assessGrade" autocomplete="off" placeholder="请输入考核评级" clearable></el-input>
        </el-form-item>
      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary"  @click="handleSubmit" v-if="type!='view'">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addObj,putObj}  from '@/api/bonus/dataorgassessment' ;
import selectTree from '@/views/bonusmanage/components/selectTree'
export default {
  name: "orgForm",
  components:{
    selectTree
  },
  props:{

  },
  watch: {

  },
  data(){
    return{
      type:'add',
      isEdit:true,
      formLoading:false,
      title:'新增',
      width:'50%',
      visible: false,
      labelWidth:'125px',
      formData: {deptCode:'', deptName:'', deptRatio:'',bonusDeptId:''},
      rules: {
        deptName: [{ required: true, message: '请输入机构名称', trigger: 'blur' },],
        deptCode: [{ required: true, message: '请输入机构编码', trigger: 'blur' }],
        // deptRatio: [{ required: true, message: '请输入机构系数', trigger: 'blur' }],
        assessItem: [{ required: true, message: '请输入考核项', trigger: 'blur' }],
        assessDate: [{ required: true, message: '请输入考核时间', trigger: 'blur' }],
        assessScore: [{ required: true, message: '请输入考核得分', trigger: 'blur' }],
        assessGrade: [{ required: true, message: '请输入考核评级', trigger: 'blur' }],
      },
    }
  },
  methods:{
   init(type,parentData,currentOrg){
      this.type = type
      this.currentOrg = currentOrg ; //父组件传过来的选择机构的信息
      this.parentData = parentData ;
      this.isEdit = this.type == 'view' ? false : true ;
      this.title = this.type == 'view' ? '查看' : this.type == 'add' ? '新增' : '编辑'
      if (type == 'add'){
        this.formData = Object.assign(this.formData,currentOrg)
      }else{
        this.formData = parentData
      }
      this.visible = true ;
    },
    handleSubmit(){
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.formLoading = true ;
          if (this.type == 'add'){
            addObj(Object.assign(this.formData,{bonusDeptId:this.currentOrg.bonusDeptId})).then(res=>{
              this.formLoading = false ;
              this.handleCancel(true) ;
            }).catch(()=>{
              this.formLoading = false ;
            })
          }else{
            putObj(this.formData).then(res=>{
              this.formLoading = false ;
              this.handleCancel(true) ;
            }).catch(()=>{
              this.formLoading = false ;
            })
          }

        } else {
          return false;
        }
      });
    },
    handleCancel(refresh){
      this.visible = false ;
      this.formData = {} ;
      if (refresh){
        this.$emit('refresh')
      }
      this.$refs.formRef.resetFields() ;
    },
    nodeClick(item){
      this.formData.parentDeptName = item.name
    },
    onChange(){

    }
  }
}
</script>

<style scoped>

</style>
